package controllers

import (
	"encoding/json"
	"ssims/models"
	"ssims/pkg/app"
	"ssims/pkg/e"
	"ssims/services"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetGener(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	supplierParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	err, info, total := services.GetAllGener(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}
func GetNumbers(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	supplierParam := map[string]interface{}{
		"page":  page,
		"limit": limit,
	}
	err, info, total := services.GetNumbers(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}
func DelGener(c *gin.Context) {
	id := ""
	if arg := c.Query("gen_id"); arg != "" {
		id = arg
	}
	if id == "" {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.DelGener(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

func UpdateGener(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	if m["GenId"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	gen_id := m["GenId"]
	gen_name := m["GenName"]
	gen_father := m["GenFather"]
	gen_mother := m["GenMother"]
	gen_no := m["GenNo"]
	// StudentTime := com.StrTo(m["StudentTime"]).MustInt()
	// PoliticalLandscape := m["PoliticalLandscape"]
	// StudentNational := m["StudentNational"]
	// DeptName := m["DeptName"]
	err := services.UpdateGener(models.Gener{
		GenId:     gen_id,
		GenName:   gen_name,
		GenFather: gen_father,
		GenMother: gen_mother,
		GenNo:     gen_no,
		Isdel:     false})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func AddGener(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	gen_id := m["GenId"]
	gen_name := m["GenName"]
	gen_father := m["GenFather"]
	gen_mother := m["GenMother"]
	gen_no := m["GenNo"]
	// StudentTime := com.StrTo(m["StudentTime"]).MustInt()
	// PoliticalLandscape := m["PoliticalLandscape"]
	// StudentNational := m["StudentNational"]
	// DeptName := m["DeptName"]
	err := services.AddGener(models.Gener{
		GenId:     gen_id,
		GenName:   gen_name,
		GenFather: gen_father,
		GenMother: gen_mother,
		GenNo:     gen_no,
		Isdel:     false})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
